<?php
/*

SHOPPERPRESS DEVELOPED BY MARK FAIL
http://www.markfail.com
http://www.shopperpress.com

Follow on twitter: http://www.twitter.com/markfail
Follow on twitter: http://www.twitter.com/shopperpress

*/

	//ini_set( 'display_errors', 1 );
	//error_reporting( E_ALL );
	if(!session_id())session_start();
 	
	// EMPTY SHOPPING CART
	if(isset($_GET['emptyCart'])){ foreach($_SESSION as $key => $value){ unset($_SESSION[$key]); }}
	 
	if(!isset($_SESSION['ddc']['cartqty'])) $_SESSION['ddc']['cartqty'] = 0;
	if(!isset($_SESSION['ddc']['price'])) $_SESSION['ddc']['price'] = 0.00;

/********************** PREMIUMPRESS THEME CONSTANTS **********************/
/**************************************************************************/

	define("PREMIUMPRESS_SYSTEM","ShopperPress");
	define("PREMIUMPRESS_VERSION","3.9.9.5");
	define("PREMIUMPRESS_VERSION_DATE","27nd April, 2010");

/********************** SHOPPERPRESS REQUIRED ***************************/
/************************************************************************/

	$functions_path = TEMPLATEPATH . '/functions/';
	define("SHOPPRESS_PATH","".$functions_path."");

	// VARIABLES
	if(defined('TEMPLATEPATH')){			
		include($functions_path ."premiumpress.php");
		include($functions_path ."premiumpress_customfields.php");
		include($functions_path ."admin-options.php");
		include($functions_path ."custom-options.php");
		include($functions_path ."func_general.php");
		include($functions_path ."func_aff.php");
		include($functions_path ."func_download.php");
		include($functions_path ."func_cart.php");
		include($functions_path ."func_image.php");
		require($functions_path ."func_callback.php");
		require($functions_path ."func_display.php");

	}	
/********************** SHOPPERPRESS ADMIN SIDEBAR  **********************/
/************************************************************************/


  	function amazon_hourly() { 		amazon_dreepfeed($ActionTime="hourly");	}
	function amazon_daily() { 		amazon_dreepfeed($ActionTime="daily");	}
	function amazon_twicedaily() { 	amazon_dreepfeed($ActionTime="twicedaily");	}
	function amazon_dreepfeed($ActionTime="hourly") {  

		global $wpdb;

		$functions_path = TEMPLATEPATH . '/functions/';

		$schedule_total = get_option("schedule_total"); $a = 1;
 
		if($schedule_total != "" && $schedule_total > 0){

			while( $a <= $schedule_total){

				$data = get_option("schedule_".$a);
 
				if($data['time'] == $ActionTime){

					$_POST['amazon'] 				= $data;
					$_POST['amazon']['keyword'] 	= $data['keyword'];
					$_POST['amazon']['keyword_cat'] = $data['keyword_cat'];
					$_POST['amazon']['cat'] 		= $data['cat'];

					// UPDATE FEED ITEM NUMBER
					$schedule= array();
					$schedule['name'] = $data['name'];
					$schedule['time'] = $data['time'];
			
					foreach($data as $key => $val){
						// UPDATE THE PAGE FOR THE NEXT SEARCH
						if($key == "start_page"){
						$schedule['start_page'] = $data['start_page']+1;
						}else{
						$schedule[$key] = $val; 
						}								
					}
					update_option("schedule_".$a, $schedule);	
					/* INCLUDE AMAZON API */
					include_once($functions_path ."amazon_api_class.php");				
					$GLOBALS['amazon_sch_search'] =1;				  
					include_once($functions_path ."amazon_search.php");
 
 
				}

			$a++; 
			}

 			wp_mail(get_option("shipping_email"), 'Amazon Import Results', "Search Name: ".$data['name']." \n\n"." Total Products Imported:".$importcounter." \n\n".' '.$emailString.'');  
		}	
		
	} 

// injects custom stylesheet into the shopperpress options page in WordPress
function admin_head() { 
	echo '<link rel="stylesheet" type="text/css" href="'.get_bloginfo('template_directory').'/functions/admin-style.css" media="screen" />';
	echo '<script type="text/javascript" src="'.get_bloginfo('template_directory').'/functions/ajax/shopperpress_ajax.js"></script>';
	echo '<script type="text/javascript" src="'.get_bloginfo('template_directory').'/functions/ajax/premiumpress.js"></script>';
 	PremiumPress_Meta();
}


 	if(!function_exists("updateQty")){ function updateQty() {
		$qty = 0;
	
		if(is_array($_SESSION['ddc']['productsincart'])){
			foreach($_SESSION['ddc']['productsincart'] as $qtyval) {
				$qty = $qty + $qtyval;
			}
		}
		$_SESSION['ddc']['cartqty'] = $qty;
		return $qty;
	} }


	if(!function_exists("updatePrice")){ function updatePrice() {
 
		global $wpdb; $total = 0; $extraPrice =0;

			if(isset($_SESSION['ddc']['productsincart']) && is_array($_SESSION['ddc']['productsincart']) ) {
				foreach($_SESSION['ddc']['productsincart'] as $product => $qty) {

				 	if($GLOBALS['shopperpress']['system'] =="credit"){
						if($_SESSION['ddc']['cartqty'] > 0){
							return get_option('credit_price_'.$_SESSION['ddc'][$product]['size']);							
						} 
				 	}else{

						if(isset($_SESSION['ddc'][$product]['main_ID'])){
							$product = $_SESSION['ddc'][$product]['main_ID'];
						} 

						if(isset($_SESSION['ddc'][$product]['extra'])){ 
							$extraPrice = $_SESSION['ddc'][$product]['extra'];
						 }

				 		 $total = $total + $extraPrice + getProductPrice($product,$product)*$qty;
					}
				}
			}
		 
		return $total;
		
	} }


	if(!function_exists("getProductPrice")){ function getProductPrice($id, $copyID="") {

		global $wpdb;
 
		if(is_numeric($copyID) && $copyID != 0){ $id = $copyID; }

		if(function_exists('get_post_meta')){			
 
			$PriceIncludingExtra = GetPrice(get_post_meta($id, "price", true));	
						
			return $PriceIncludingExtra;	
		} 
	  }
	}

function javascript_footer(){ ?>

	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/_ShopperPress.js"></script> <!-- shopperpress custom -->
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/functions/ajax/shopperpress_ajax.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/jquery-1.3.2.min.js"></script>
    <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/jquery-ui-1.7.1.custom.min.js"></script>
	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/superfish.js"></script> <!-- menu styles -->
	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/ddcart.php?sid=<?php echo session_id(); ?>"></script>
    <script type="text/javascript">	$(function(){$('ul.sf-menu').superfish();});</script>

 	<?php if($GLOBALS['shopperpress']['subnav'] =="yes" && $GLOBALS['shopperpress']['theme'] !="default"){ ?>

	<script type="text/javascript">

       $(document).ready(function() {
            $(".dropdown img.flag").addClass("flagvisibility");

            $(".dropdown dt a").click(function() {
                $(".dropdown dd ul").toggle();
            });
                        
            $(".dropdown dd ul li a").click(function() {
                var text = $(this).html();
                $(".dropdown dt a span").html(text);
                $(".dropdown dd ul").hide();
               // $("#result").html("Selected value is: " + getSelectedValue("sample"));
            });


            $(".dropdown1 img.flag").addClass("flagvisibility");

            $(".dropdown1 dt a").click(function() {
                $(".dropdown1 dd ul").toggle();
            });
                        
            $(".dropdown1 dd ul li a").click(function() {
                var text = $(this).html();
                $(".dropdown1 dt a span").html(text);
                $(".dropdown1 dd ul").hide();
               // $("#result").html("Selected value is: " + getSelectedValue("sample"));
            });
                        
            function getSelectedValue(id) {
                return $("#" + id).find("dt a span.value").html();
            }

            $(document).bind('click', function(e) {
                var $clicked = $(e.target);
                if (! $clicked.parents().hasClass("dropdown"))
                    $(".dropdown dd ul").hide();
				if (! $clicked.parents().hasClass("dropdown1"))
                    $(".dropdown1 dd ul").hide();
            });


            $("#flagSwitcher").click(function() {
                $(".dropdown img.flag").toggleClass("flagvisibility");
            });
        });
	</script>

	<?php } ?>

	<?php if(is_home() && get_option("display_featuredbox") =="1" ){ ?>
		<script src="<?php bloginfo('template_url'); ?>/scripts/s3Slider.js" type="text/javascript"></script> 
		
		<script type="text/javascript">	 
			$(document).ready(function() {			
				$('#featured-item').s3Slider({
					timeOut: 6000 
				});									 
			}); 
		</script>
	<?php } ?>

	<?php if(is_single()){ ?>
	 
	<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/scripts/jquery.lightbox-0.5.js"></script>
    <link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/jquery.lightbox-0.5.css" media="screen" />
	<script type="text/javascript">
    $(function() {
        $('#gallery a').lightBox();
    });
    </script>
	<?php } ?>
    
    
    <?php if(is_page()){ ?>
    
    <script language="JavaScript">
 
	function formCheck(formobj){
	// Enter name of mandatory fields
	var fieldRequired = Array("billing[fname]", "billing[lname]", "billing[address1]", "billing[phone]", "billing[city]", "billing[zip]");
	// Enter field description to appear in the dialog box
	var fieldDescription = Array("First Name", "Last Name","Address","Phone","City", "Zip/Post Code");
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

		if (alertMsg.length == l_Msg){
			return true;
		}else{
			alert(alertMsg);
			return false;
		}
	}
	// -->
	</script>
    <?php } ?>
    
<script type="text/javascript">function handleError() {return true;}window.onerror = handleError;</script>
<?php }
 
/**
 * Sets up the javascript AJAX environment.
 */
function javascript_ajax_header( ) { 
	// Define custom JavaScript function

	$ShopperPressTheme = $GLOBALS['shopperpress']['theme'];
	if(strlen($ShopperPressTheme) < 2){ $ShopperPressTheme="default"; }
	?>
    
    <?php echo stripslashes(get_option("google_webmaster_code")); ?>
	<style type="text/css" media="screen">
		@import url( <?php bloginfo('template_url'); ?>/style.css );		 
	</style>
	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
	<?php wp_get_archives('type=monthly&format=link'); ?>

	<link href="<?php bloginfo('template_url'); ?>/themes/<?php echo $ShopperPressTheme; ?>/css/styles.css" type="text/css" rel="stylesheet" />
	<!--[if IE 6]>
	<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/themes/ie6.css" />
	<![endif]-->
	<!--[if IE 7]>
	<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/themes/ie7.css" />
	<![endif]-->
 	<!--[if IE 8]>
	<link rel="stylesheet" type="text/css" href="<?php bloginfo('template_url'); ?>/themes/ie8.css" />
	<![endif]-->
	<?php
}

 
	

if(defined('TEMPLATEPATH')){

	// hook into WordPress
	add_action('wp_head', 'javascript_ajax_header' );
	add_action('admin_head', 'admin_head');
	add_action('admin_menu', 'shopperpress_options'); 
 
	// CHECK FOR DAILY SCHEDUALS
	 if (!wp_next_scheduled('amazon_search')) {  
		wp_schedule_event( time(), 'hourly', 'amazon_search' );  
		wp_schedule_event( time(), 'twicedaily', 'amazon_twicedaily' );
		wp_schedule_event( time(), 'daily', 'amazon_daily' );
	 }
  
	add_action( 'amazon_search', 'amazon_dreepfeed' ); 
	
	// WORDPRESS RECOMMENDED REMOVALS
	remove_action('wp_head', 'rsd_link');
	remove_action('wp_head', 'wlwmanifest_link');
	remove_action('wp_head', 'wp_generator');
 	
	// activate support for .mo localization files
	load_theme_textdomain('cp');

}

/*

Register the widget functions for allowing sidebar plugins.

*/

if ( function_exists('register_sidebar') ){

register_sidebar(array('name'=>'Left Sidebar',
	'before_widget' => '<div style="clear:both;"></div><div class="menubox_box">',
	'after_widget' => '</div>',
	'before_title' => '<h2><span>',
	'after_title' => '</span></h2>',
));

register_sidebar(array('name'=>'Right Sidebar',
	'before_widget' => '<div style="clear:both;"></div><div class="menubox_box">',
	'after_widget' => '</div>',
	'before_title' => '<h2><span>',
	'after_title' => '</span></h2>',
));


register_sidebar(array('name'=>'Footer',
	'before_widget' => '<div class="Footer_Widget">',
	'after_widget' => '</div>',
	'before_title' => '<div style="" class="bar3"><h2>',
	'after_title' => '</h2></div>',
));
}
?>